﻿@page "/"
@inherits BasePage

<PageTitle>@AppConfig.Branch - @Language["App.SubTitle"]</PageTitle>

<GridRow><GridCol Span="24"><KnownCard /></GridCol></GridRow>
<GridRow><GridCol Span="24"><SpaceCard Counts="counts" /></GridCol></GridRow>
<GridRow>
    <GridCol Span="14">
        <div class="ws-chart" style="margin-right:10px;">
            <Card Style="height:100%;">
                <CardTabs>
                    <Tabs DefaultActiveKey="1">
                        <TabPane Key="1">
                            <TabTemplate>@Language["Home.LogStatistic"]</TabTemplate>
                            <ChildContent>
                                <AntDesign.Charts.Column Config="chartConfig" Data="chartData" />
                            </ChildContent>
                        </TabPane>
                    </Tabs>
                </CardTabs>
            </Card>
        </div>
    </GridCol>
    <GridCol Span="10">
        <CommFuncCard Menus="visitMenus" />
    </GridCol>
</GridRow>

@code {
    private object[] chartData;
    private List<MenuInfo> visitMenus;
    private ColumnConfig chartConfig;
    private List<StatisticCountInfo> counts = [];

    protected override async Task OnPageInitAsync()
    {
        chartConfig = new()
        {
            Name = Language["Home.VisitTitle"].Replace("{month}", $"{DateTime.Now:yyyyMM}"),
            Height = 274,
            Padding = "auto",
            XField = "key",
            YField = "value",
            Meta = new
            {
                Key = new { Alias = Language["Home.Date"] },
                Value = new { Alias = Language["Home.Count"] }
            }
        };

        var service = new Services.HomeService(Context);
        var info = await service.GetHomeAsync();
        counts.Add(new StatisticCountInfo { Name = Language["Home.UserCount"], Count = info?.Statistics?.UserCount });
        counts.Add(new StatisticCountInfo { Name = Language["Home.LogCount"], Count = info?.Statistics?.LogCount });
        chartData = info?.Statistics?.LogDatas?.FirstOrDefault()?.Series.Select(d => new { d.Key, d.Value }).ToArray();
        visitMenus = Context.GetMenus(info?.VisitMenuIds);
    }

    // //第三方登录设置当前用户
    // protected override async Task OnAfterRenderAsync(bool firstRender)
    // {
    //    if (firstRender)
    //        await SetCurrentUserAsync(CurrentUser);
    //    await base.OnAfterRenderAsync(firstRender);
    // }

    // //第三方登录获取当前用户
    // protected override async Task<UserInfo> GetThirdUserAsync()
    // {
    //    //var third = ThirdApi.GetUser();
    //    var third = new { UserName = "admin" };
    //    var user = await Platform.GetUserAsync(third.UserName);
    //    return user;
    // }
}